home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995 August: Tool Chest / Dev.CD Aug 95 TC / Dev.CD Aug 95 TC.toast / Tool Chest / Development Tools & Languages / Dylan Related / Marlais / Marlais 0.5.9-portable sources / gc / include / gc.h < prev   
Encoding:
C/C++ Source or Header  |  1995-03-15  |  28.2 KB  |  665 lines  |  [TEXT/ttxt]

  1. /* 
  2.  * Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers
  3.  * Copyright (c) 1991-1994 by Xerox Corporation.  All rights reserved.
  4.  *
  5.  * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
  6.  * OR IMPLIED.  ANY USE IS AT YOUR OWN RISK.
  7.  *
  8.  * Permission is hereby granted to use or copy this program
  9.  * for any purpose,  provided the above notices are retained on all copies.
  10.  * Permission to modify the code and to distribute modified code is granted,
  11.  * provided the above notices are retained, and a notice that the code was
  12.  * modified is included with the above copyright notice.
  13.  */
  14. /* Boehm, January 28, 1995 3:59 pm PST */
  15.  
  16. /*
  17.  * Note that this defines a large number of tuning hooks, which can
  18.  * safely be ignored in nearly all cases.  For normal use it suffices
  19.  * to call only GC_MALLOC and perhaps GC_REALLOC.
  20.  * For better performance, also look at GC_MALLOC_ATOMIC, and
  21.  * GC_enable_incremental.  If you need an action to be performed
  22.  * immediately before an object is collected, look at GC_register_finalizer.
  23.  * If you are using Solaris threads, look at the end of this file.
  24.  * Everything else is best ignored unless you encounter performance
  25.  * problems.
  26.  */
  27.  
  28. #ifndef _GC_H
  29.  
  30. # define _GC_H
  31.  
  32. # ifdef __cplusplus
  33.     extern "C" {
  34. # endif
  35.  
  36. # include <stddef.h>
  37.  
  38. /* Define word and signed_word to be unsigned and signed types of the     */
  39. /* size as char * or void *.  There seems to be no way to do this    */
  40. /* even semi-portably.  The following is probably no better/worse     */
  41. /* than almost anything else.                        */
  42. /* The ANSI standard suggests that size_t and ptr_diff_t might be     */
  43. /* better choices.  But those appear to have incorrect definitions    */
  44. /* on may systems.  Notably "typedef int size_t" seems to be both    */
  45. /* frequent and WRONG.                            */
  46. typedef unsigned long GC_word;
  47. typedef long GC_signed_word;
  48.  
  49. /* Public read-only variables */
  50.  
  51. extern GC_word GC_gc_no;/* Counter incremented per collection.      */
  52.             /* Includes empty GCs at startup.        */
  53.             
  54.  
  55. /* Public R/W variables */
  56.  
  57. extern int GC_quiet;    /* Disable statistics output.  Only matters if    */
  58.             /* collector has been compiled with statistics    */
  59.             /* enabled.  This involves a performance cost,    */
  60.             /* and is thus not the default.            */
  61.  
  62. extern int GC_dont_gc;    /* Dont collect unless explicitly requested, e.g. */
  63.             /* beacuse it's not safe.              */
  64.  
  65. extern int GC_dont_expand;
  66.             /* Dont expand heap unless explicitly requested */
  67.             /* or forced to.                */
  68.  
  69. extern int GC_full_freq;    /* Number of partial collections between    */
  70.                 /* full collections.  Matters only if    */
  71.                 /* GC_incremental is set.            */
  72.             
  73. extern GC_word GC_non_gc_bytes;
  74.             /* Bytes not considered candidates for collection. */
  75.             /* Used only to control scheduling of collections. */
  76.  
  77. extern GC_word GC_free_space_divisor;
  78.             /* We try to make sure that we allocate at     */
  79.             /* least N/GC_free_space_divisor bytes between    */
  80.             /* collections, where N is the heap size plus    */
  81.             /* a rough estimate of the root set size.    */
  82.             /* Initially, GC_free_space_divisor = 4.    */
  83.             /* Increasing its value will use less space    */
  84.             /* but more collection time.  Decreasing it    */
  85.             /* will appreciably decrease collection time    */
  86.             /* at the expense of space.            */
  87.             /* GC_free_space_divisor = 1 will effectively    */
  88.             /* disable collections.                */
  89.             
  90.             
  91. /* Public procedures */
  92. /*
  93.  * general purpose allocation routines, with roughly malloc calling conv.
  94.  * The atomic versions promise that no relevant pointers are contained
  95.  * in the object.  The nonatomic versions guarantee that the new object
  96.  * is cleared.  GC_malloc_stubborn promises that no changes to the object
  97.  * will occur after GC_end_stubborn_change has been called on the
  98.  * result of GC_malloc_stubborn. GC_malloc_uncollectable allocates an object
  99.  * that is scanned for pointers to collectable objects, but is not itself
  100.  * collectable.  GC_malloc_uncollectable and GC_free called on the resulting
  101.  * object implicitly update GC_non_gc_bytes appropriately.
  102.  */
  103. # if defined(__STDC__) || defined(__cplusplus)
  104.   extern void * GC_malloc(size_t size_in_bytes);
  105.   extern void * GC_malloc_atomic(size_t size_in_bytes);
  106.   extern void * GC_malloc_uncollectable(size_t size_in_bytes);
  107.   extern void * GC_malloc_stubborn(size_t size_in_bytes);
  108. # else
  109.   extern char * GC_malloc(/* size_in_bytes */);
  110.   extern char * GC_malloc_atomic(/* size_in_bytes */);
  111.   extern char * GC_malloc_uncollectable(/* size_in_bytes */);
  112.   extern char * GC_malloc_stubborn(/* size_in_bytes */);
  113. # endif
  114.  
  115. #if defined(__STDC__) && !defined(__cplusplus)
  116. # define NO_PARAMS void
  117. #else
  118. # define NO_PARAMS
  119. #endif
  120.  
  121. /* Explicitly deallocate an object.  Dangerous if used incorrectly.     */
  122. /* Requires a pointer to the base of an object.                */
  123. /* If the argument is stubborn, it should not be changeable when freed. */
  124. /* An object should not be enable for finalization when it is         */
  125. /* explicitly deallocated.                        */
  126. /* GC_free(0) is a no-op, as required by ANSI C for free.        */
  127. # if defined(__STDC__) || defined(__cplusplus)
  128.   extern void GC_free(void * object_addr);
  129. # else
  130.   extern void GC_free(/* object_addr */);
  131. # endif
  132.  
  133. /*
  134.  * Stubborn objects may be changed only if the collector is explicitly informed.
  135.  * The collector is implicitly informed of coming change when such
  136.  * an object is first allocated.  The following routines inform the
  137.  * collector that an object will no longer be changed, or that it will
  138.  * once again be changed.  Only nonNIL pointer stores into the object
  139.  * are considered to be changes.  The argument to GC_end_stubborn_change
  140.  * must be exacly the value returned by GC_malloc_stubborn or passed to
  141.  * GC_change_stubborn.  (In the second case it may be an interior pointer
  142.  * within 512 bytes of the beginning of the objects.)
  143.  * There is a performance penalty for allowing more than
  144.  * one stubborn object to be changed at once, but it is acceptable to
  145.  * do so.  The same applies to dropping stubborn objects that are still
  146.  * changeable.
  147.  */
  148. void GC_change_stubborn(/* p */);
  149. void GC_end_stubborn_change(/* p */);
  150.  
  151. /* Return a pointer to the base (lowest address) of an object given    */
  152. /* a pointer to a location within the object.                */
  153. /* Return 0 if displaced_pointer doesn't point to within a valid    */
  154. /* object.                                */
  155. # if defined(__STDC__) || defined(__cplusplus)
  156.   void * GC_base(void * displaced_pointer);
  157. # else
  158.   char * GC_base(/* char * displaced_pointer */);
  159. # endif
  160.  
  161. /* Given a pointer to the base of an object, return its size in bytes.    */
  162. /* The returned size may be slightly larger than what was originally    */
  163. /* requested.                                */
  164. # if defined(__STDC__) || defined(__cplusplus)
  165.   size_t GC_size(void * object_addr);
  166. # else
  167.   size_t GC_size(/* char * object_addr */);
  168. # endif
  169.  
  170. /* For compatibility with C library.  This is occasionally faster than    */
  171. /* a malloc followed by a bcopy.  But if you rely on that, either here    */
  172. /* or with the standard C library, your code is broken.  In my        */
  173. /* opinion, it shouldn't have been invented, but now we're stuck. -HB    */
  174. /* The resulting object has the same kind as the original.        */
  175. /* If the argument is stubborn, the result will have changes enabled.    */
  176. /* It is an error to have changes enabled for the original object.    */
  177. /* Follows ANSI comventions for NULL old_object.            */
  178. # if defined(__STDC__) || defined(__cplusplus)
  179.     extern void * GC_realloc(void * old_object, size_t new_size_in_bytes);
  180. # else
  181.     extern char * GC_realloc(/* old_object, new_size_in_bytes */);
  182. # endif
  183.  
  184.  
  185. /* Explicitly increase the heap size.    */
  186. /* Returns 0 on failure, 1 on success.  */
  187. extern int GC_expand_hp(/* number_of_bytes */);
  188.  
  189. /* Limit the heap size to n bytes.  Useful when you're debugging,     */
  190. /* especially on systems that don't handle running out of memory well.    */
  191. /* n == 0 ==> unbounded.  This is the default.                */
  192. extern void GC_set_max_heap_size(/* n */);
  193.  
  194. /* Clear the set of root segments.  Wizards only. */
  195. extern void GC_clear_roots(NO_PARAMS);
  196.  
  197. /* Add a root segment.  Wizards only. */
  198. extern void GC_add_roots(/* low_address, high_address_plus_1 */);
  199.  
  200. /* Add a displacement to the set of those considered valid by the    */
  201. /* collector.  GC_register_displacement(n) means that if p was returned */
  202. /* by GC_malloc, then (char *)p + n will be considered to be a valid    */
  203. /* pointer to n.  N must be small and less than the size of p.        */
  204. /* (All pointers to the interior of objects from the stack are        */
  205. /* considered valid in any case.  This applies to heap objects and    */
  206. /* static data.)                            */
  207. /* Preferably, this should be called before any other GC procedures.    */
  208. /* Calling it later adds to the probability of excess memory        */
  209. /* retention.                                */
  210. /* This is a no-op if the collector was compiled with recognition of    */
  211. /* arbitrary interior pointers enabled, which is now the default.    */
  212. void GC_register_displacement(/* GC_word n */);
  213.  
  214. /* The following version should be used if any debugging allocation is    */
  215. /* being done.                                */
  216. void GC_debug_register_displacement(/* GC_word n */);
  217.  
  218. /* Explicitly trigger a full, world-stop collection.     */
  219. void GC_gcollect(NO_PARAMS);
  220.  
  221. /* Trigger a full world-stopped collection.  Abort the collection if     */
  222. /* and when stop_func returns a nonzero value.  Stop_func will be     */
  223. /* called frequently, and should be reasonably fast.  This works even    */
  224. /* if virtual dirty bits, and hence incremental collection is not     */
  225. /* available for this architecture.  Collections can be aborted faster    */
  226. /* than normal pause times for incremental collection.  However,    */
  227. /* aborted collections do no useful work; the next collection needs    */
  228. /* to start from the beginning.                        */
  229. typedef int (* GC_stop_func)(NO_PARAMS);
  230. # if defined(__STDC__) || defined(__cplusplus)
  231.     int GC_try_to_collect(GC_stop_func stop_func);
  232. # else
  233.     int GC_try_to_collect(/* GC_stop_func stop_func */);
  234. # endif
  235.  
  236. /* Return the number of bytes in the heap.  Excludes collector private    */
  237. /* data structures.  Includes empty blocks and fragmentation loss.    */
  238. /* Includes some pages that were allocated but never written.        */
  239. size_t GC_get_heap_size(NO_PARAMS);
  240.  
  241. /* Return the number of bytes allocated since the last collection.    */
  242. size_t GC_get_bytes_since_gc(NO_PARAMS);
  243.  
  244. /* Enable incremental/generational collection.    */
  245. /* Not advisable unless dirty bits are         */
  246. /* available or most heap objects are        */
  247. /* pointerfree(atomic) or immutable.        */
  248. /* Don't use in leak finding mode.        */
  249. /* Ignored if GC_dont_gc is true.        */
  250. void GC_enable_incremental(NO_PARAMS);
  251.  
  252. /* Perform some garbage collection work, if appropriate.    */
  253. /* Return 0 if there is no more work to be done.        */
  254. /* Typically performs an amount of work corresponding roughly    */
  255. /* to marking from one page.  May do more work if further    */
  256. /* progress requires it, e.g. if incremental collection is    */
  257. /* disabled.  It is reasonable to call this in a wait loop    */
  258. /* until it returns 0.                        */
  259. int GC_collect_a_little(NO_PARAMS);
  260.  
  261. /* Allocate an object of size lb bytes.  The client guarantees that    */
  262. /* as long as the object is live, it will be referenced by a pointer    */
  263. /* that points to somewhere within the first 256 bytes of the object.    */
  264. /* (This should normally be declared volatile to prevent the compiler    */
  265. /* from invalidating this assertion.)  This routine is only useful    */
  266. /* if a large array is being allocated.  It reduces the chance of     */
  267. /* accidentally retaining such an array as a result of scanning an    */
  268. /* integer that happens to be an address inside the array.  (Actually,    */
  269. /* it reduces the chance of the allocator not finding space for such    */
  270. /* an array, since it will try hard to avoid introducing such a false    */
  271. /* reference.)  On a SunOS 4.X or MS Windows system this is recommended */
  272. /* for arrays likely to be larger than 100K or so.  For other systems,    */
  273. /* or if the collector is not configured to recognize all interior    */
  274. /* pointers, the threshold is normally much higher.            */
  275. # if defined(__STDC__) || defined(__cplusplus)
  276.   void * GC_malloc_ignore_off_page(size_t lb);
  277. # else
  278.   char * GC_malloc_ignore_off_page(/* size_t lb */);
  279. # endif
  280. # if defined(__STDC__) || defined(__cplusplus)
  281.   void * GC_malloc_atomic_ignore_off_page(size_t lb);
  282. # else
  283.   char * GC_malloc_atomic_ignore_off_page(/* size_t lb */);
  284. # endif
  285.  
  286. /* Debugging (annotated) allocation.  GC_gcollect will check         */
  287. /* objects allocated in this way for overwrites, etc.            */
  288. # if defined(__STDC__) || defined(__cplusplus)
  289.   extern void * GC_debug_malloc(size_t size_in_bytes,
  290.                   char * descr_string, int descr_int);
  291.   extern void * GC_debug_malloc_atomic(size_t size_in_bytes,
  292.                          char * descr_string, int descr_int);
  293.   extern void * GC_debug_malloc_uncollectable(size_t size_in_bytes,
  294.                              char * descr_string, int descr_int);
  295.   extern void * GC_debug_malloc_stubborn(size_t size_in_bytes,
  296.                            char * descr_string, int descr_int);
  297.   extern void GC_debug_free(void * object_addr);
  298.   extern void * GC_debug_realloc(void * old_object,
  299.                     size_t new_size_in_bytes,
  300.                     char * descr_string, int descr_int);
  301. # else
  302.   extern char * GC_debug_malloc(/* size_in_bytes, descr_string, descr_int */);
  303.   extern char * GC_debug_malloc_atomic(/* size_in_bytes, descr_string,
  304.                         descr_int */);
  305.   extern char * GC_debug_malloc_uncollectable(/* size_in_bytes, descr_string,
  306.                         descr_int */);
  307.   extern char * GC_debug_malloc_stubborn(/* size_in_bytes, descr_string,
  308.                         descr_int */);
  309.   extern void GC_debug_free(/* object_addr */);
  310.   extern char * GC_debug_realloc(/* old_object, new_size_in_bytes,
  311.                           descr_string, descr_int */);
  312. # endif
  313. void GC_debug_change_stubborn(/* p */);
  314. void GC_debug_end_stubborn_change(/* p */);
  315. # ifdef GC_DEBUG
  316. #   define GC_MALLOC(sz) GC_debug_malloc(sz, __FILE__, __LINE__)
  317. #   define GC_MALLOC_ATOMIC(sz) GC_debug_malloc_atomic(sz, __FILE__, __LINE__)
  318. #   define GC_MALLOC_UNCOLLECTABLE(sz) GC_debug_malloc_uncollectable(sz, \
  319.                             __FILE__, __LINE__)
  320. #   define GC_REALLOC(old, sz) GC_debug_realloc(old, sz, __FILE__, \
  321.                                    __LINE__)
  322. #   define GC_FREE(p) GC_debug_free(p)
  323. #   define GC_REGISTER_FINALIZER(p, f, d, of, od) \
  324.     GC_register_finalizer(GC_base(p), GC_debug_invoke_finalizer, \
  325.                   GC_make_closure(f,d), of, od)
  326. #   define GC_REGISTER_FINALIZER_IGNORE_SELF(p, f, d, of, od) \
  327.     GC_register_finalizer_ignore_self( \
  328.              GC_base(p), GC_debug_invoke_finalizer, \
  329.          GC_make_closure(f,d), of, od)
  330. #   define GC_MALLOC_STUBBORN(sz) GC_debug_malloc_stubborn(sz, __FILE__, \
  331.                                    __LINE__)
  332. #   define GC_CHANGE_STUBBORN(p) GC_debug_change_stubborn(p)
  333. #   define GC_END_STUBBORN_CHANGE(p) GC_debug_end_stubborn_change(p)
  334. #   define GC_GENERAL_REGISTER_DISAPPEARING_LINK(link, obj) \
  335.     GC_general_register_disappearing_link(link, GC_base(obj))
  336. #   define GC_REGISTER_DISPLACEMENT(n) GC_debug_register_displacement(n)
  337. # else
  338. #   define GC_MALLOC(sz) GC_malloc(sz)
  339. #   define GC_MALLOC_ATOMIC(sz) GC_malloc_atomic(sz)
  340. #   define GC_MALLOC_UNCOLLECTABLE(sz) GC_malloc_uncollectable(sz)
  341. #   define GC_REALLOC(old, sz) GC_realloc(old, sz)
  342. #   define GC_FREE(p) GC_free(p)
  343. #   define GC_REGISTER_FINALIZER(p, f, d, of, od) \
  344.     GC_register_finalizer(p, f, d, of, od)
  345. #   define GC_REGISTER_FINALIZER_IGNORE_SELF(p, f, d, of, od) \
  346.     GC_register_finalizer_ignore_self(p, f, d, of, od)
  347. #   define GC_MALLOC_STUBBORN(sz) GC_malloc_stubborn(sz)
  348. #   define GC_CHANGE_STUBBORN(p) GC_change_stubborn(p)
  349. #   define GC_END_STUBBORN_CHANGE(p) GC_end_stubborn_change(p)
  350. #   define GC_GENERAL_REGISTER_DISAPPEARING_LINK(link, obj) \
  351.     GC_general_register_disappearing_link(link, obj)
  352. #   define GC_REGISTER_DISPLACEMENT(n) GC_register_displacement(n)
  353. # endif
  354. /* The following are included because they are often convenient, and    */
  355. /* reduce the chance for a misspecifed size argument.  But calls may    */
  356. /* expand to something syntactically incorrect if t is a complicated    */
  357. /* type expression.                              */
  358. # define GC_NEW(t) (t *)GC_MALLOC(sizeof (t))
  359. # define GC_NEW_ATOMIC(t) (t *)GC_MALLOC_ATOMIC(sizeof (t))
  360. # define GC_NEW_STUBBORN(t) (t *)GC_MALLOC_STUBBORN(sizeof (t))
  361. # define GC_NEW_UNCOLLECTABLE(t) (t *)GC_MALLOC_UNCOLLECTABLE(sizeof (t))
  362.  
  363. /* Finalization.  Some of these primitives are grossly unsafe.        */
  364. /* The idea is to make them both cheap, and sufficient to build        */
  365. /* a safer layer, closer to PCedar finalization.            */
  366. /* The interface represents my conclusions from a long discussion    */
  367. /* with Alan Demers, Dan Greene, Carl Hauser, Barry Hayes,         */
  368. /* Christian Jacobi, and Russ Atkinson.  It's not perfect, and        */
  369. /* probably nobody else agrees with it.        Hans-J. Boehm  3/13/92    */
  370. # if defined(__STDC__) || defined(__cplusplus)
  371.   typedef void (*GC_finalization_proc)(void * obj, void * client_data);
  372. # else
  373.   typedef void (*GC_finalization_proc)(/* void * obj, void * client_data */);
  374. # endif
  375.  
  376. # if defined(__STDC__) || defined(__cplusplus)
  377.     void GC_register_finalizer(void * obj,
  378.                    GC_finalization_proc fn, void * cd,
  379.                    GC_finalization_proc *ofn, void ** ocd);
  380. # else    
  381.     void GC_register_finalizer(/* void * obj,
  382.                       GC_finalization_proc fn, void * cd,
  383.                       GC_finalization_proc *ofn, void ** ocd */);
  384. # endif
  385.     /* When obj is no longer accessible, invoke        */
  386.     /* (*fn)(obj, cd).  If a and b are inaccessible, and    */
  387.     /* a points to b (after disappearing links have been    */
  388.     /* made to disappear), then only a will be        */
  389.     /* finalized.  (If this does not create any new        */
  390.     /* pointers to b, then b will be finalized after the    */
  391.     /* next collection.)  Any finalizable object that    */
  392.     /* is reachable from itself by following one or more    */
  393.     /* pointers will not be finalized (or collected).    */
  394.     /* Thus cycles involving finalizable objects should    */
  395.     /* be avoided, or broken by disappearing links.        */
  396.     /* Fn should terminate as quickly as possible, and    */
  397.     /* defer extended computation.                */
  398.     /* All but the last finalizer registered for an object  */
  399.     /* is ignored.                        */
  400.     /* Finalization may be removed by passing 0 as fn.    */
  401.     /* The old finalizer and client data are stored in    */
  402.     /* *ofn and *ocd.                    */ 
  403.     /* Fn is never invoked on an accessible object,        */
  404.     /* provided hidden pointers are converted to real     */
  405.     /* pointers only if the allocation lock is held, and    */
  406.     /* such conversions are not performed by finalization    */
  407.     /* routines.                        */
  408.     /* If GC_register_finalizer is aborted as a result of    */
  409.     /* a signal, the object may be left with no        */
  410.     /* finalization, even if neither the old nor new    */
  411.     /* finalizer were NULL.                    */
  412.     /* Obj should be the nonNULL starting address of an     */
  413.     /* object allocated by GC_malloc or friends.        */
  414.     /* Note that any garbage collectable object referenced    */
  415.     /* by cd will be considered accessible until the    */
  416.     /* finalizer is invoked.                */
  417.  
  418. /* Another versions of the above follow.  It ignores        */
  419. /* self-cycles, i.e. pointers from a finalizable object to    */
  420. /* itself.  There is a stylistic argument that this is wrong,    */
  421. /* but it's unavoidable for C++, since the compiler may        */
  422. /* silently introduce these.  It's also benign in that specific    */
  423. /* case.                            */
  424. # if defined(__STDC__) || defined(__cplusplus)
  425.     void GC_register_finalizer_ignore_self(void * obj,
  426.                    GC_finalization_proc fn, void * cd,
  427.                    GC_finalization_proc *ofn, void ** ocd);
  428. # else    
  429.     void GC_register_finalizer_ignore_self(/* void * obj,
  430.                       GC_finalization_proc fn, void * cd,
  431.                       GC_finalization_proc *ofn, void ** ocd */);
  432. # endif
  433.  
  434. /* The following routine may be used to break cycles between    */
  435. /* finalizable objects, thus causing cyclic finalizable        */
  436. /* objects to be finalized in the correct order.  Standard    */
  437. /* use involves calling GC_register_disappearing_link(&p),    */
  438. /* where p is a pointer that is not followed by finalization    */
  439. /* code, and should not be considered in determining         */
  440. /* finalization order.                        */
  441. # if defined(__STDC__) || defined(__cplusplus)
  442.     int GC_register_disappearing_link(void ** link);
  443. # else
  444.     int GC_register_disappearing_link(/* void ** link */);
  445. # endif
  446.  
  447.     /* Link should point to a field of a heap allocated     */
  448.     /* object obj.  *link will be cleared when obj is    */
  449.     /* found to be inaccessible.  This happens BEFORE any    */
  450.     /* finalization code is invoked, and BEFORE any        */
  451.     /* decisions about finalization order are made.        */
  452.     /* This is useful in telling the finalizer that     */
  453.     /* some pointers are not essential for proper        */
  454.     /* finalization.  This may avoid finalization cycles.    */
  455.     /* Note that obj may be resurrected by another        */
  456.     /* finalizer, and thus the clearing of *link may    */
  457.     /* be visible to non-finalization code.          */
  458.     /* There's an argument that an arbitrary action should  */
  459.     /* be allowed here, instead of just clearing a pointer. */
  460.     /* But this causes problems if that action alters, or     */
  461.     /* examines connectivity.                */
  462.     /* Returns 1 if link was already registered, 0        */
  463.     /* otherwise.                        */
  464.     /* Only exists for backward compatibility.  See below:    */
  465. # if defined(__STDC__) || defined(__cplusplus)
  466.     int GC_general_register_disappearing_link(void ** link, void * obj);
  467. # else
  468.     int GC_general_register_disappearing_link(/* void ** link, void * obj */);
  469. # endif
  470.     /* A slight generalization of the above. *link is    */
  471.     /* cleared when obj first becomes inaccessible.  This    */
  472.     /* can be used to implement weak pointers easily and    */
  473.     /* safely. Typically link will point to a location    */
  474.     /* holding a disguised pointer to obj.  (A pointer     */
  475.     /* inside an "atomic" object is effectively          */
  476.     /* disguised.)   In this way soft            */
  477.     /* pointers are broken before any object        */
  478.     /* reachable from them are finalized.  Each link    */
  479.     /* May be registered only once, i.e. with one obj    */
  480.     /* value.  This was added after a long email discussion */
  481.     /* with John Ellis.                    */
  482.     /* Obj must be a pointer to the first word of an object */
  483.     /* we allocated.  It is unsafe to explicitly deallocate */
  484.     /* the object containing link.  Explicitly deallocating */
  485.     /* obj may or may not cause link to eventually be    */
  486.     /* cleared.                        */
  487. # if defined(__STDC__) || defined(__cplusplus)
  488.     int GC_unregister_disappearing_link(void ** link);
  489. # else
  490.     int GC_unregister_disappearing_link(/* void ** link */);
  491. # endif
  492.     /* Returns 0 if link was not actually registered.    */
  493.     /* Undoes a registration by either of the above two    */
  494.     /* routines.                        */
  495.  
  496. /* Auxiliary fns to make finalization work correctly with displaced    */
  497. /* pointers introduced by the debugging allocators.            */
  498. # if defined(__STDC__) || defined(__cplusplus)
  499.     void * GC_make_closure(GC_finalization_proc fn, void * data);
  500.     void GC_debug_invoke_finalizer(void * obj, void * data);
  501. # else
  502.     char * GC_make_closure(/* GC_finalization_proc fn, char * data */);
  503.     void GC_debug_invoke_finalizer(/* void * obj, void * data */);
  504. # endif
  505.  
  506. /* GC_set_warn_proc can be used to redirect or filter warning messages.    */
  507. # if defined(__STDC__) || defined(__cplusplus)
  508.     typedef void (*GC_warn_proc)(char *msg, GC_word arg);
  509.     GC_warn_proc GC_set_warn_proc(GC_warn_proc p);
  510.     /* Returns old warning procedure.    */
  511. # else
  512.     typedef void (*GC_warn_proc)(/* char *msg, GC_word arg */);
  513.     GC_warn_proc GC_set_warn_proc(/* GC_warn_proc p */);
  514. # endif
  515.     
  516. /* The following is intended to be used by a higher level    */
  517. /* (e.g. cedar-like) finalization facility.  It is expected    */
  518. /* that finalization code will arrange for hidden pointers to    */
  519. /* disappear.  Otherwise objects can be accessed after they    */
  520. /* have been collected.                        */
  521. /* Note that putting pointers in atomic objects or in         */
  522. /* nonpointer slots of "typed" objects is equivalent to     */
  523. /* disguising them in this way, and may have other advantages.    */
  524. # ifdef I_HIDE_POINTERS
  525. #   if defined(__STDC__) || defined(__cplusplus)
  526. #     define HIDE_POINTER(p) (~(size_t)(p))
  527. #     define REVEAL_POINTER(p) ((void *)(HIDE_POINTER(p)))
  528. #   else
  529. #     define HIDE_POINTER(p) (~(unsigned long)(p))
  530. #     define REVEAL_POINTER(p) ((char *)(HIDE_POINTER(p)))
  531. #   endif
  532.     /* Converting a hidden pointer to a real pointer requires verifying    */
  533.     /* that the object still exists.  This involves acquiring the      */
  534.     /* allocator lock to avoid a race with the collector.        */
  535.  
  536. #   if defined(__STDC__) || defined(__cplusplus)
  537.         typedef void * (*GC_fn_type)();
  538.         void * GC_call_with_alloc_lock(GC_fn_type fn, void * client_data);
  539. #   else
  540.         typedef char * (*GC_fn_type)();
  541.         char * GC_call_with_alloc_lock(/* GC_fn_type fn, char * client_data */);
  542. #   endif
  543. # endif
  544.  
  545. /* Check that p and q point to the same object.          */
  546. /* Fail conspicuously if they don't.                */
  547. /* Returns the first argument.                  */
  548. /* Succeeds if neither p nor q points to the heap.        */
  549. /* May succeed if both p and q point to between heap objects.    */
  550. #ifdef __STDC__
  551.   void * GC_same_obj(register void *p, register void *q);
  552. #else
  553.   char * GC_same_obj(/* char * p, char * q */);
  554. #endif
  555.  
  556. /* Check that p is visible                        */
  557. /* to the collector as a possibly pointer containing location.        */
  558. /* If it isn't fail conspicuously.                    */
  559. /* Returns the argument in all cases.  May erroneously succeed        */
  560. /* in hard cases.  (This is intended for debugging use with        */
  561. /* untyped allocations.  The idea is that it should be possible, though    */
  562. /* slow, to add such a call to all indirect pointer stores.)        */
  563. /* Currently useless for multithreaded worlds.                */
  564. #ifdef __STDC__
  565.   void * GC_is_visible(void *p);
  566. #else
  567.   char *GC_is_visible(/* char * p */);
  568. #endif
  569.  
  570. /* Check that if p is a pointer to a heap page, then it points to    */
  571. /* a valid displacement within a heap object.                */
  572. /* Fail conspicuously if this property does not hold.            */
  573. /* Uninteresting with ALL_INTERIOR_POINTERS.                */
  574. /* Always returns its argument.                        */
  575. #ifdef __STDC__
  576.   void * GC_is_valid_displacement(void *p);
  577. #else
  578.   char *GC_is_valid_displacement(/* char * p */);
  579. #endif
  580.  
  581. /* Safer, but slow, pointer addition.  Probably useful mainly with     */
  582. /* a preprocessor.  Useful only for heap pointers.            */
  583. #ifdef GC_DEBUG
  584. #   define GC_PTR_ADD3(x, n, type_of_result) \
  585.     ((type_of_result)GC_same_obj((x)+(n), (x)))
  586. #   ifdef __GNUC__
  587. #       define GC_PTR_ADD(x, n) \
  588.         ((typeof(x))GC_same_obj((x)+(n), (x)))
  589. #   else
  590.     /* We can't do this right without typeof, which ANSI    */
  591.     /* decided was not sufficiently useful.  Repeatedly    */
  592.     /* mentioning the arguments seems too dangerous to be    */
  593.     /* useful.  So does not casting the result.        */
  594. #       define GC_PTR_ADD(x, n) ((x)+(n))
  595. #   endif
  596. #else    /* !GC_DEBUG */
  597. #   define GC_PTR_ADD3(x, n, type_of_result) ((x)+(n))
  598. #   define GC_PTR_ADD(x, n) ((x)+(n))
  599. #endif
  600.  
  601. /* Safer assignment of a pointer to a nonstack location.    */
  602. #ifdef GC_DEBUG
  603. # ifdef __STDC__
  604. #   define GC_PTR_STORE(p, q) \
  605.     (*(void **)GC_is_visible(p) = GC_is_valid_displacement(q))
  606. # else
  607. #   define GC_PTR_STORE(p, q) \
  608.     (*(char **)GC_is_visible(p) = GC_is_valid_displacement(q))
  609. # endif
  610. #else /* !GC_DEBUG */
  611. #   define GC_PTR_STORE(p, q) *((p) = (q))
  612. #endif
  613.  
  614.  
  615. #ifdef SOLARIS_THREADS
  616. /* We need to intercept calls to many of the threads primitives, so     */
  617. /* that we can locate thread stacks and stop the world.            */
  618. /* Note also that the collector cannot see thread specific data.    */
  619. /* Thread specific data should generally consist of pointers to        */
  620. /* uncollectable objects, which are deallocated using the destructor    */
  621. /* facility in thr_keycreate.                        */
  622. # include <thread.h>
  623. # include <signal.h>
  624.   int GC_thr_create(void *stack_base, size_t stack_size,
  625.                     void *(*start_routine)(void *), void *arg, long flags,
  626.                     thread_t *new_thread);
  627.   int GC_thr_join(thread_t wait_for, thread_t *departed, void **status);
  628.   int GC_thr_suspend(thread_t target_thread);
  629.   int GC_thr_continue(thread_t target_thread);
  630.   void * GC_dlopen(const char *path, int mode);
  631.  
  632. # define thr_create GC_thr_create
  633. # define thr_join GC_thr_join
  634. # define thr_suspend GC_thr_suspend
  635. # define thr_continue GC_thr_continue
  636. # define dlopen GC_dlopen
  637.  
  638. /* This returns a list of objects, linked through their first        */
  639. /* word.  Its use can greatly reduce lock contention problems, since    */
  640. /* the allocation lock can be acquired and released many fewer times.    */
  641. void * GC_malloc_many(size_t lb);
  642. #define GC_NEXT(p) (*(void **)(p))     /* Retrieve the next element    */
  643.                     /* in returned list.        */
  644.  
  645. #endif /* SOLARIS_THREADS */
  646.  
  647. /*
  648.  * If you are planning on putting
  649.  * the collector in a SunOS 5 dynamic library, you need to call GC_INIT()
  650.  * from the statically loaded program section.
  651.  * This circumvents a Solaris 2.X (X<=4) linker bug.
  652.  */
  653. #ifdef sparc
  654. #   define GC_INIT() { extern end, etext; \
  655.                GC_noop(&end, &etext); }
  656. #else
  657. #   define GC_INIT()
  658. #endif
  659.  
  660. #ifdef __cplusplus
  661.     }  /* end of extern "C" */
  662. #endif
  663.  
  664. #endif /* _GC_H */
  665.